/*
 * PermissionsDialog.java
 *
 * Created on September 28, 2007, 7:38 PM
 */

package org.glossitope.container.security.permissions;

import java.awt.Frame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.glossitope.container.MainPanel;

/**
 *
 * @author  joshy
 */
public class PermissionsDialog extends javax.swing.JDialog {

    public static int showConfirmDialog(MainPanel mainPanel, String widgetName, String permType, String description) {
        Frame frame = (Frame)SwingUtilities.getWindowAncestor(mainPanel);
        PermissionsDialog dialog = new PermissionsDialog(frame, true);
        dialog.permissionName.setText(permType);
        dialog.widgetName.setText(widgetName);
        dialog.reasonText.setText(description);
        dialog.pack();
        dialog.setLocation(frame.getWidth()/2-dialog.getWidth()/2,
                (frame.getHeight()-dialog.getHeight())/2);
        dialog.setVisible(true);
        return dialog.getSelectedValue();
    }
    private int selectedValue = -1;
    
    /** Creates new form PermissionsDialog */
    public PermissionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.getRootPane().setDefaultButton(this.permanentButton);
    }

    private int getSelectedValue() {
        return selectedValue;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        widgetName = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        permissionName = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        reasonText = new javax.swing.JTextArea();
        permanentButton = new javax.swing.JButton();
        denyButton = new javax.swing.JButton();
        tempButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("The widget");

        widgetName.setText("jLabel2");

        jLabel3.setText("would like access to:");

        permissionName.setText("jLabel4");

        jLabel5.setText("Reason:");

        reasonText.setColumns(20);
        reasonText.setEditable(false);
        reasonText.setLineWrap(true);
        reasonText.setRows(5);
        reasonText.setText("This is some crazy cool text:\n");
        reasonText.setWrapStyleWord(true);
        jScrollPane1.setViewportView(reasonText);

        permanentButton.setText("Approve Permanently");
        permanentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                permanentButtonActionPerformed(evt);
            }
        });

        denyButton.setText("Deny");
        denyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                denyButtonActionPerformed(evt);
            }
        });

        tempButton.setText("Approve");
        tempButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tempButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel1)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(widgetName)
                    .add(permissionName)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(84, Short.MAX_VALUE)
                .add(tempButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(denyButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(permanentButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(widgetName))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(permissionName))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(denyButton)
                    .add(tempButton)
                    .add(permanentButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void permanentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_permanentButtonActionPerformed
        this.selectedValue = JOptionPane.YES_OPTION;
        this.setVisible(false);
    }//GEN-LAST:event_permanentButtonActionPerformed

    private void denyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_denyButtonActionPerformed
        // TODO add your handling code here:
        this.selectedValue = JOptionPane.NO_OPTION;
        this.setVisible(false);
    }//GEN-LAST:event_denyButtonActionPerformed

    private void tempButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tempButtonActionPerformed
        this.selectedValue = JOptionPane.YES_OPTION;
        this.setVisible(false);
    }//GEN-LAST:event_tempButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PermissionsDialog dialog = new PermissionsDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton denyButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton permanentButton;
    private javax.swing.JLabel permissionName;
    private javax.swing.JTextArea reasonText;
    private javax.swing.JButton tempButton;
    private javax.swing.JLabel widgetName;
    // End of variables declaration//GEN-END:variables
    
}
